package com.liu.java8;

import java.util.Arrays;
import java.util.List;

public class StreamDemo {
    public static void main(String[] args) {
        User u1 = new User(1, "a", 21);
        User u2 = new User(2, "b", 22);
        User u3 = new User(3, "c", 23);
        User u4 = new User(4, "d", 24);
        User u5 = new User(5, "e", 25);
        User u6 = new User(6, "f", 26);
        List<User> list = Arrays.asList(u1, u2, u3, u4, u5, u6);

        list.stream()
                .filter((user) -> {
                    // 筛选出ID为偶数的用户
                    return user.getId() % 2 == 0;
                })
                .filter((user) -> {
                    // 筛选出Age大于23的用户
                    return user.getAge() > 23;
                })
                .map((user) -> {
                    // 用户名称大写
                    return user.getName().toUpperCase();
                })
                .sorted((user1, user2) -> {
                    // 排序
                    return user1.compareTo(user2);
                })
                // 指定输出个数
                .limit(1)
                // 遍历
                .forEach(System.out::println);

    }
}

class User {
    private Integer id;
    private String name;
    private Integer age;

    public User(Integer id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
